/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.GuiManager;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.BadLocationException;

/**
 *
 * @author Jojo
 */
public class StructureCard extends JPanel{
    ImageIcon dasBild;
    private Image img;
    int structureId = 0;
   
    public StructureCard(URL pDateipfad, int pStructureId){
        this.structureId = pStructureId;
     
            //BufferedImage myPicture = ImageIO.read(new File(pDateipfad));
            dasBild = new ImageIcon( pDateipfad );
            this.img = dasBild.getImage();
            Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
		this.setLayout(null);
		this.setPreferredSize(size);
		// setMinimumSize(size);
		// setMaximumSize(size);
		this.setSize(size);
                
                this.addMouseListener(new MouseAdapter(){
                    public void mouseClicked(MouseEvent e){
                        if(e.getClickCount()==2 && e.getButton() == 1)
                        cardClicked();
                    }
                });
            
            
        
        

    }
     	

	@Override
	public void paintComponent(Graphics g) {
		g.drawImage(img, 0, 0, null);
	}
        
        void cardClicked(){
            String intend = "";
            try {
            intend = GuiManager.getActivePane().getText(GuiManager.getActivePane().getCaretPosition(), GuiManager.getActivePane().getCaretOffsetFromLineStart());
            intend = intend.replaceFirst("\n", "");
        } catch (BadLocationException ex) {
            Logger.getLogger(StructureCard.class.getName()).log(Level.SEVERE, null, ex);
        }
            String outputText = "";
            switch (this.structureId) {
            case 1:  outputText = "if( Bedingung ){\n"
                    + intend +  "\t"+ "\t"+ "DeineAnweisungen;"
                    + "\n"
                    + intend + "\t"
                    + "}";
                     break;
            case 2:  outputText = "if( Bedingung ){\n"
                    + intend + "\t"+ "\t"+ "DeineAnweisungen;"
                    + "\n"
                    + intend +"\t"
                    +"}\n"
                    + intend + "\t" + "else{\n"
                    + intend + "\t" +"\t" +"DeineAnweisungen;"
                    + "\n"
                    + intend +"\t" + "}";
                     break;
            case 3:  outputText = "while( Bedingung ){\n"
                    + intend +  "\t"+ "\t"+ "DeineAnweisungen;"
                    + "\n"
                    + intend + "\t"
                    + "}";
                     break;
            case 4:  outputText = "do{\n"
                    + intend + "\t"+ "\t"+ "DeineAnweisungen;"
                    + "\n"
                    + intend +"\t"
                    +"}\n"
                    + intend + "\t" + "while( Bedingung );";
                     break;
            case 5:  outputText = "while( true ){\n"
                    + intend +  "\t"+ "\t"+ "DeineAnweisungen;"
                    + "\n"
                    + intend + "\t"
                    + "}";
                     break;
            case 6: outputText = "for( int i=0; i<10; i++ ){\n"
                    + intend +  "\t"+ "\t"+ "DeineAnweisungen;"
                    + "\n"
                    + intend + "\t"
                    + "}";
                    break;
            case 7:  outputText = "import egLejos.*;"+"\n"+"\n"
                    + "class NameDerKlasse  {"
                + "\n"+ "\n"+ "\n"+ "\t"                
                + "void start(){\n"
                + "\t\t" + "DeineAnweisungen;\n"
                + "\t}"+ "\n"+ "\n"+"\t"
                + "public static void main(String[] args){\n"
                + "\t\t" + "new NameDerKlasse().start();\n"
                + "\t}" + "\n"+ "\n"+ "}";
                     break;
  
            default: outputText = "Invalid month";
                     break;
        }

        GuiManager.getActivePane().insert(outputText, GuiManager.getActivePane().getCaretPosition());
             
             
        }
    
}
